<?php
/**
 * 后台管理服务
 * Created by PhpStorm.
 * User: crstu
 * Date: 2016/4/25
 * Time: 17:28
 */

namespace backend\models\Service;

use yii;
use common\models\CommonFunction;
use common\models\DAO\Admin;
use common\models\DAO\AdminPower;
use common\models\DAO\AdminLog;

class AdminService
{

    /**
     * 登录操作
     * 注：1：登陆成功记录日志，登录错误记录日志；
     *    2：session操作
     * @param string $admin_name 用户名
     * @param string $admin_pass 密码
     * @param string $verify_code 验证码
     * @return array|bool array(status, msg)
     */
    public function login($admin_name, $admin_pass, $verify_code){

        $session = \Yii::$app->session;

        //检查验证码正确
        $session_captcha = $session->get('__captcha/login/captcha');
        $session->set('__captcha/login/captcha', CommonFunction::getRandChar(10));//每次刷新验证码Session
        if(empty($verify_code) || $verify_code != $session_captcha) {
            return [0, '验证码错误！'];
        }

        $adminLog = new AdminLog();
        $login_ip = CommonFunction::get_client_ip();

        //检查IP是否暴力登录
        if($adminLog->checkForceLogin($admin_name, $login_ip)){

            $admin = Admin::find()->where(['adminName' => $admin_name])->one();
            if(empty($admin)){
                return [0, '登录名无效'];
            }else{
                if($admin->status < 1){
                    return [0, '管理员被禁用'];
                }else{

                    //密码验证
                    if($admin->validatePassword($admin_pass)){

                        //保存登录日志
                        AdminLog::addLoginLog($admin_name, 1);

                        //更新管理员最后登录信息
                        $admin->updateLastLogin();

                        //Session和Cookies操作
                        $session->set('ADMIN_ID', $admin->id);
                        $session->set('ADMIN_NAME', $admin->adminName);
                        $session->set('ADMIN_REAL_NAME', $admin->realName);
                        $session->set('ADMIN_ROLE_ID', $admin->roleID);
                        
                        return [1, '登录成功！', 'admin_id' => $admin->id];

                    }else{

                        //保存登录日志
                        AdminLog::addLoginLog($admin_name, 0);

                        return [0, '密码错误'];
                    }
                }
            }
        }else{
            return [0, '登录次数过多！'];
        }
    }

    /**
     * 初始化权限
     * @param int $role_id 用户组ID
     * @return array 菜单数组
     */
    public static function initPower($role_id){
        $role_id = intval($role_id);
        $all_menu = \Yii::$app->params['admin_menu'];
        //超级管理员
        if($role_id > 1){
            $role_powers = AdminPower::find()->select(['controller', 'action'])->where(['roleID'=>$role_id])->asArray()->all();
            if(!empty($role_powers)){
                foreach($all_menu as $m => $module){
                    foreach($module['child'] as $k => $child){
                        //删除不符合权限的菜单项
                        if(!in_array(array('controller'=>$child[0], 'action'=>$child[1]), $role_powers)){
                            unset($all_menu[$m]['child'][$k]);
                        }
                    }
                    if(empty($all_menu[$m])) unset($all_menu[$m]); //删除空菜单组
                }
            }else{
                return null;
            }
        }
        foreach($all_menu as $m => $module){
            foreach($module['child'] as $k => $child){
                //删除不显示的菜单项
                if(!$child[3]){
                    unset($all_menu[$m]['child'][$k]);
                }
            }
            if(empty($all_menu[$m]['child'])) unset($all_menu[$m]); //删除空菜单组
        }
        return $all_menu;
    }

    
}